package com.example.guideapp.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.guideapp.R;
import com.example.guideapp.bean.WishType;
import com.makeramen.roundedimageview.RoundedImageView;

import java.util.List;

/**
 * Created by Administrator on 2017/10/18.
 */

public class MyWishTypeGridViewAdapter extends BaseAdapter {

    private Context context;
    private List<WishType> list;

    private LayoutInflater mInflater;

    private WishType wishType;

    public MyWishTypeGridViewAdapter(Context context, List<WishType> list) {
        this.context = context;
        this.list = list;;

        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        if (list != null){
            return list.size();
        }else {
            return 0;
        }
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        WishTypeViewHolder viewHolder;

        if (convertView == null){
            convertView = mInflater.inflate(R.layout.fragment2_gridview_item,null);

            viewHolder = new WishTypeViewHolder();
            viewHolder.wishType_icon = (RoundedImageView) convertView.findViewById(R.id.iv2_wishType_icon);
            viewHolder.wishType_name = (TextView) convertView.findViewById(R.id.tv2_wishType_name);

            convertView.setTag(viewHolder);
        }else {
            viewHolder = (WishTypeViewHolder) convertView.getTag();
        }

        wishType = list.get(position);

        byte[] icon = wishType.getWishIcon();
        if (icon!= null){
            Bitmap bitmap = BitmapFactory.decodeByteArray(icon,0,icon.length);
            viewHolder.wishType_icon.setImageBitmap(bitmap);
        }else {
            viewHolder.wishType_icon.setImageResource(R.mipmap.no);
        }

        viewHolder.wishType_name.setText(wishType.getWishName());

        return convertView;
    }

    static class WishTypeViewHolder{
        private RoundedImageView wishType_icon;
        private TextView wishType_name;
    }
}
